﻿// Test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "PropertyData.h"

void toBinaryFile(const char* txtFile, const char* binFile, const char* fmt, char mode)
{
	//const char *txtFile = "ctyw.txt", *binFile = "ctyw.dat";
	FILE* pTxtFile, * pBinFile;
	errno_t errT, errB;
	errT = fopen_s(&pTxtFile, txtFile, "r");
	errB = fopen_s(&pBinFile, binFile, "wb");
	if (errT || errB)
	{
		printf("open %s 或 %s failed\n", txtFile, binFile);
		return;
	}

	rewind(pTxtFile);
	//const char *fmt = "%lf %s %s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %s\n";

	while (!feof(pTxtFile))
	{
		switch (mode) {
		case 'C':
		case 'c':
			CTYW ctyw;
			fscanf_s(pTxtFile, fmt,
				&ctyw.id, ctyw.province, 20, ctyw.city, 20, &ctyw.ta_year, &ctyw.ta_day_5, &ctyw.ta_day_8,
				&ctyw.ta_month_h, &ctyw.ta_ac, &ctyw.phi, &ctyw.w_winter_m, &ctyw.w_winter,
				&ctyw.w_summer, &ctyw.ta_lowest, &ctyw.ta_highest, ctyw.styear, 10);
			fwrite(&ctyw, sizeof(ctyw), 1, pBinFile);
			break;
		case 'I':
		case 'i':
			Insul insul;
			fscanf_s(pTxtFile, fmt,
				&insul.id, insul.name, 40, insul.remark, 20, &insul.density_min, &insul.density_max, &insul.allowTemperature_min,
				&insul.allowTemperature_max, &insul.reTemperature_min, &insul.reTemperature_max, &insul.tc0,
				&insul.tm_min, &insul.tm_max, &insul.a, &insul.b, &insul.c, &insul.d, &insul.e, &insul.f, &insul.g,
				&insul.strength, &insul.strength_196);
			fwrite(&insul, sizeof(insul), 1, pBinFile);
			break;
		case 'M':
		case 'm':
			PipeMaterialProperties pmp;
			fscanf_s(pTxtFile, "%u %s %s %s %s %s %s %s %s %s %u %u %s %u %u %u",
				&pmp.id, pmp.stressCode, 20, pmp.ShapeCode, 20, pmp.MClass,50, pmp.ClassCode, 20,
				pmp.pipeName, 40, pmp.weldingCode, 20, pmp.pipeCode, 20, pmp.MCode, 25, pmp.process, 25,
				&pmp.thickness_min, &pmp.thickness_max, pmp.temperature_min, 20, &pmp.rm,
				&pmp.rel, &pmp.temperature_max);
			for (size_t i = 0; i < 36; i++)
			{
				fscanf_s(pTxtFile, " %lf", &pmp.stress[i]);
			}
			fscanf_s(pTxtFile, " %lf %s", &pmp.stress_tmax, pmp.remark, 20);
			fwrite(&pmp, sizeof(pmp), 1, pBinFile);
			break;
		default:
			printf("%s\n", "mode 'c':for CTYW, 'i':for Insul, 'm' for PipeMaterialProperties");
		}

	}

	fclose(pTxtFile);
	fclose(pBinFile);
}

int main(int argc, char const* argv[])
{	
	for (int i = 0; i < argc; ++i)
	{
		if (!strcmp("C", argv[i]))
		{
			const char* fmt = "%lf %s %s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %s\n";
			const char* txtFile = "ctyw.txt", * binFile = "ctyw.dat";
			toBinaryFile(txtFile, binFile, fmt, 'c');
		}
		if (!strcmp("I", argv[i]))
		{
			const char* fmt = "%u %s %s %u %u %d %d %d %d %lf %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf\n";
			const char* txtFile = "Insulation.txt", * binFile = "Insulation.dat";
			toBinaryFile(txtFile, binFile, fmt, 'i');
		}
		if (!strcmp("M", argv[i]))
		{
			const char* fmt = "";
			const char* txtFile = "Material.txt", * binFile = "Material.dat";
			toBinaryFile(txtFile, binFile, fmt, 'm');
		}
		if (!strcmp("RC", argv[i]))
		{
			const char* filePath = "ctyw.dat";
			CTYW* ctyw = (CTYW*)malloc(sizeof(CTYW));
			getPropertyData(filePath, ctyw, 0, 1);
			printf("%f %s\n", ctyw->id, ctyw->city);
		}
		if (!strcmp("RI", argv[i]))
		{
			const char* filePath = "Insulation.dat";
			Insul* insul = (Insul*)malloc(sizeof(Insul));
			getPropertyData(filePath, insul, 0, 1);
			printf("%u %s\n", insul->id, insul->name);
		}
		if (!strcmp("RM", argv[i]))
		{
			const char* filePath = "Material.dat";
			PipeMaterialProperties* pmp = (PipeMaterialProperties*)malloc(sizeof(PipeMaterialProperties));
			getPropertyData(filePath, pmp, 0, 1);
			printf("%u %s\n", pmp->id, pmp->pipeName);
		}
		if (!strcmp("RC-A", argv[i]))
		{
			const char* filePath = "ctyw.dat";
			CTYW* ctyw = (CTYW*)malloc(sizeof(CTYW) * 255);
			int row = getPropertyData(filePath, ctyw, 0, 0);
			for (int i = 0; i < row; ++i)
			{
				printf("%f %s\n", (ctyw+i)->id, (ctyw+i)->city);
			}
			free(ctyw);
		}
		if (!strcmp("RI-A", argv[i]))
		{
			const char* filePath = "Insulation.dat";
			Insul* insul = (Insul*)malloc(sizeof(Insul) * 255);
			int row = getPropertyData(filePath, insul, 0, 0);
			for (int i = 0; i < row; ++i)
			{
				printf("%u %s\n", (insul+i)->id, (insul+i)->name);
			}
			free(insul);
		}
		if (!strcmp("RM-A", argv[i]))
		{
			const char* filePath = "Material.dat";
			PipeMaterialProperties* pmp = (PipeMaterialProperties*)malloc(sizeof(PipeMaterialProperties) * 258);
			int row = getPropertyData(filePath, pmp, 0, 256);
			for (int i = 0; i < row; ++i)
			{
				printf("%u %s %s %s %s\n", (pmp+i)->id, (pmp + i)->pipeName, (pmp + i)->pipeCode, (pmp + i)->MCode, (pmp + i)->remark);
			}
			free(pmp);
		}
	}

	return 0;
}
